<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\GuideAdv;
use App\Models\SystemRegion;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class GuideAdvController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GuideAdv(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('img') ->image('',60,60);
            $grid->column('region_id')->display(function($region_id){
                if($region_id){
                    $systemRegion = SystemRegion::find($region_id);

                    return  $systemRegion ? $systemRegion -> region_name : '';
                }else{
                    return '全部';
                }
            });
//            $grid->column('url');
            $grid->column('second');
            $grid->column('is_show') ->switch();
            $grid->column('online_at')->width('200px');
            $grid->column('offline_at')->width('200px');

        
            $grid ->disableViewButton();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GuideAdv(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('img');
            $show->field('region_id');
            $show->field('url');
            $show->field('second');
            $show->field('is_show');
            $show->field('online_at');
            $show->field('offline_at');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GuideAdv(), function (Form $form) {
            $form->display('id');
            $form->text('title');
            $form->image('img')->move('adv/'.date('Y-m-d')) ->autoUpload();

            $region = \App\Admin\Repositories\SystemRegion::getList();
            $form->select('region_id')->options($region) ->default(0, true);
            $form->text('url');
            $form->number('second');
            $form->radio('is_show')->options([ 0 => '下架', 1 => '上架']) ->default(0);
            $form->datetime('online_at');
            $form->datetime('offline_at');
        
            $form->display('created_at');
            $form->display('updated_at');

            $form->submitted(function($form){
                $id  = $form->builder()->getResourceId();

                $guideAdv = \App\Models\GuideAdv::find($id);

                if($guideAdv -> is_show != $form -> is_show){
                    if($form -> is_show == 1){

                        $form -> online_at = date('Y-m-d H:i:s',time());
                    }else{

                        $form -> offline_at = date('Y-m-d H:i:s',time());
                    }
                }

                if (!$form->region_id) {
                    $form->region_id = 0;
                }
            });

        });
    }
}
